/*
 * Copyright 2006 Berry Crawford
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.judo.service.systemservices;

import org.judo.application.Application;
import org.judo.service.JudoService;

public class LoggingService implements JudoService,Logger
{
	private boolean noLog4j = false;
	
	public void debug(Object obj,Object message)
	{
		try
		{
			if (noLog4j)
				System.out.println("[DEBUG] ("+obj.getClass().getCanonicalName()+"): "+message);
			else
				Log4JLogger.debug(obj,message.toString());
		}
		catch(Throwable e)
		{
			noLog4j = true;
			System.out.println("[DEBUG] ("+obj.getClass().getCanonicalName()+"): "+message);
		}
	}
	
	public void info(Object obj,Object message)
	{
		try
		{
			if (noLog4j)
				System.out.println("[INFO] ("+obj.getClass().getCanonicalName()+"): "+message);
			else
				Log4JLogger.info(obj,message.toString());
		}
		catch(Throwable e)
		{
			noLog4j = true;
			System.out.println("[INFO] ("+obj.getClass().getCanonicalName()+"): "+message);
		}
	}
	
	public void warn(Object obj,Object message)
	{
		try
		{
			if (noLog4j)
				System.out.println("[WARN] ("+obj.getClass().getCanonicalName()+"): "+message);
			else
				Log4JLogger.warn(obj,message.toString());
		}
		catch(Throwable e)
		{
			noLog4j = true;
			System.err.println("[WARN] ("+obj.getClass().getCanonicalName()+"): "+message);
		}	
	}
	
	public void error(Object obj,Object message)
	{
		try
		{
			if (noLog4j)
				System.err.println("[ERROR] ("+obj.getClass().getCanonicalName()+"): "+message);
			else
				Log4JLogger.error(obj,message.toString());
		}
		catch(Throwable e)
		{
			noLog4j = true;
			System.err.println("[ERROR] ("+obj.getClass().getCanonicalName()+"): "+message);
		}
	}
	
	public void fatal(Object obj,Object message)
	{
		try
		{
			if (noLog4j)
				System.err.println("[FATAL] ("+obj.getClass().getCanonicalName()+"): "+message);
			else
				Log4JLogger.fatal(obj,message.toString());
		}
		catch(Throwable e)
		{
			noLog4j = true;
			System.err.println("[FATAL] ("+obj.getClass().getCanonicalName()+"): "+message);
		}	
	}

	public void init()
	{
		try
		{
			Log4JLogger.info(this,"Using Log4j for logging");
		}
		catch(Throwable e)
		{
			noLog4j = true;
			System.err.println("[WARN] ("+this.getClass().getCanonicalName()+"): Could not find Log4J.  Using 'out' and 'err' instead.");
		}
	}
}
